package com.smit.livevideo.sample.dialog;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

import android.app.DialogFragment;
import android.app.FragmentManager;

import com.smit.utils.LogUtil;

/**
 * Created by smfang on 2015/12/28.
 * DialogFragment工具类
 */
public final class DialogFragmentUtil
{
    /** CI MMI Menu dialog的key */
    public static final String CI_MMI_MENU_DIALOG = "CIMMIMenuDialog";
    /** CI MMI Pin dialog的key */
    public static final String CI_MMI_PIN_DIALOG = "CIMMIPINDialog";

    private static Stack<DialogFragment> dialogFragments = new Stack<DialogFragment>();
    private static Map<String, BaseDialogFragment> mBaseDialogMap = new HashMap<String, BaseDialogFragment>();

    private DialogFragmentUtil()
    {

    }

    /**
     * 获取CI MMI Menu dialog对象
     * @return BaseDialogFragment
     */
    public static BaseDialogFragment getCIMMIMenuDialog()
    {
        BaseDialogFragment ciMenuDialog = mBaseDialogMap.get(CI_MMI_MENU_DIALOG);
        if (ciMenuDialog == null)
        {
            ciMenuDialog = new CIMMIMenuDialog();
            mBaseDialogMap.put(CI_MMI_MENU_DIALOG, ciMenuDialog);
        }

        return ciMenuDialog;
    }

    /**
     * 获取CI MMI Pin dialog对象
     * @return BaseDialogFragment
     */
    public static BaseDialogFragment getCIMMIPinDialog()
    {
        BaseDialogFragment ciMMIEnqDialog = mBaseDialogMap.get(CI_MMI_PIN_DIALOG);
        if (ciMMIEnqDialog == null)
        {
            ciMMIEnqDialog = new CIMMIPinDialog();
            mBaseDialogMap.put(CI_MMI_PIN_DIALOG, ciMMIEnqDialog);
        }

        return ciMMIEnqDialog;
    }

    /**
     * 显示dialog
     * @param manager FragmentManager对象
     * @param dialog BaseDialogFragment对象
     * @param tag dialog的key
     */
    public static void showDialog(FragmentManager manager, BaseDialogFragment dialog, String tag)
    {
        dialog.show(manager, tag);
    }

    /**
     * 关闭dialog
     * @param manager FragmentManager对象
     * @param tag 关闭的dialog的key
     */
    public static void dismissDialogByTag(FragmentManager manager, String tag)
    {
        BaseDialogFragment baseDialog = (BaseDialogFragment) manager.findFragmentByTag(tag);
        if (baseDialog != null)
        {
            baseDialog.dismiss();
        }
    }

    /**
     * 添加dialog到栈里面去
     * @param dialogFragment DialogFragment对象
     */
    public static void addDialogFragment(DialogFragment dialogFragment)
    {
        dialogFragments.add(dialogFragment);
    }

    /**
     * 弹出最上面的dialog
     * @return DialogFragment对象
     */
    public static DialogFragment popDialogFragment()
    {
        if (dialogFragments.size() > 0)
        {
            return dialogFragments.pop();
        }
        else
        {
            return null;
        }

    }

    /**
     * 获取堆栈中最上层的dialog
     * @return DialogFragment对象
     */
    public static DialogFragment peekDialogFragment()
    {
        if (dialogFragments.size() > 0)
        {
            return dialogFragments.peek();
        }
        else
        {
            return null;
        }
    }

    /**
     * 关闭所有dialog
     */
    public static void dismissAllDialogFragment()
    {
        for (DialogFragment dialog : dialogFragments)
        {
            LogUtil.debug("test", "dismissAllDialogFragment dialog=" + dialog);
            if (dialog.isAdded())
            {
                dialog.dismiss();
            }
        }
        dialogFragments.clear();
    }
}
